{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas-时期极度频率转换，时期时间戳转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "内容介绍:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import datetime\n",
    "import pandas.tseries.offsets as offset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.季度时期频率的转换\n",
    "\n",
    "许多季度数据会涉及“财年末”的概念，通常是一年12个月中某月的最后一个工作日或日历日。\n",
    "\n",
    "* pandas支持12种可能的季度频率，即Q-JAN到Q-DEC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2018Q4', 'Q-JAN')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# （1）财政年度和季度年度\n",
    "# 以1月结束的2018财政年度(对应日历年度的2017.2-2018.1)的每个季度最后一个月的最后公历日\n",
    "# 这个时期的范围2017-2到2018-1，每三个月一个季度，这里的Q4代表第四季度\n",
    "p3 = pd.Period('2018Q4',freq='Q-JAN')\n",
    "p3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017-11', 'M')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#p3是2018财政年度的第四季度，转换为月度计量，数据显示开始月份\n",
    "p3.asfreq('M',how='S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2018-01', 'M')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#p3是2018财政年度的第四季度，转换为月度计量，数据显示结束月份(默认)\n",
    "p3.asfreq('M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2018-01-30 16:00', 'T')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# （2）该季度倒数第二个工作日的下午4点时间戳\n",
    "# p.asfreq('B','e')得出该季度最后一个月的最后一个工作日\n",
    "# 'B'：日期-工作日频率；'E'：时期的结尾(how参数)\n",
    "# 'T':时间频率的分钟；'S':开始(how参数)\n",
    "p4pm = (p3.asfreq('B','E')-1).asfreq('T','s')+16*60\n",
    "p4pm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2018-01-30 16:00:00')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#转成时间戳\n",
    "p4pm.to_timestamp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2017Q3    0\n",
       "2017Q4    1\n",
       "2018Q1    2\n",
       "2018Q2    3\n",
       "2018Q3    4\n",
       "2018Q4    5\n",
       "Freq: Q-JAN, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# （3）相同的运算可以应用到TimeSeries\n",
    "rng = pd.period_range('2017Q3','2018Q4',freq='Q-Jan')\n",
    "ts = pd.Series(np.arange(len(rng)),index=rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-10-28 16:00:00    0\n",
       "2017-01-30 16:00:00    1\n",
       "2017-04-27 16:00:00    2\n",
       "2017-07-28 16:00:00    3\n",
       "2017-10-30 16:00:00    4\n",
       "2018-01-30 16:00:00    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_rng = (rng.asfreq('B','E')-1).asfreq('T','s')+16*60\n",
    "ts.index = new_rng.to_timestamp()\n",
    "ts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.TimeStamp与Period互相转换\n",
    "\n",
    "* 通过to_period方法，可以将时间戳(timestamp)索引的Series和DataFrame对象转换为以时期(period)索引\n",
    "* 也可以将timestamp转换为period出现重复时期\n",
    "* to_timestamp转换为timestamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-01-31    0\n",
       "2018-02-28    1\n",
       "2018-03-31    2\n",
       "Freq: M, dtype: int64"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (1)通过to_period方法，可以将时间戳(timestamp)索引的Series和DataFrame对象转换为以时期(period)索引\n",
    "rng4 = pd.date_range('2018-01-01',periods=3,freq='M')\n",
    "ts4 = pd.Series(np.arange(len(rng4)),index=rng4)\n",
    "ts4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31'], dtype='datetime64[ns]', freq='M')"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts4.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-01    0\n",
       "2018-02    1\n",
       "2018-03    2\n",
       "Freq: M, dtype: int64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用to_period函数转换成时期索引\n",
    "pts = ts4.to_period()\n",
    "pts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2018-01', '2018-02', '2018-03'], dtype='period[M]', freq='M')"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pts.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-12-29    0\n",
       "2018-12-30    1\n",
       "2018-12-31    2\n",
       "2019-01-01    3\n",
       "2019-01-02    4\n",
       "2019-01-03    5\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (2)将timestamp转换为period出现重复时期\n",
    "rng42 = pd.date_range('12/29/2018', periods=6, freq='D')\n",
    "ts42 = pd.Series(np.arange(len(rng42)),index=rng42)\n",
    "ts42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-12    0\n",
       "2018-12    1\n",
       "2018-12    2\n",
       "2019-01    3\n",
       "2019-01    4\n",
       "2019-01    5\n",
       "Freq: M, dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp = ts42.to_period('M')\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2018-12', '2018-12', '2018-12', '2019-01', '2019-01', '2019-01'], dtype='period[M]', freq='M')"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-12-31 23:59:59.999999999    0\n",
       "2018-12-31 23:59:59.999999999    1\n",
       "2018-12-31 23:59:59.999999999    2\n",
       "2019-01-31 23:59:59.999999999    3\n",
       "2019-01-31 23:59:59.999999999    4\n",
       "2019-01-31 23:59:59.999999999    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (3)to_timestamp可以将period转换为timestamp\n",
    "# 时期数据转换回来时会失真\n",
    "temp.to_timestamp(how='E')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "asfreq(...)\n",
      "    Convert Period to desired frequency, at the start or end of the interval.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    freq : str\n",
      "        The desired frequency.\n",
      "    how : {'E', 'S', 'end', 'start'}, default 'end'\n",
      "        Start or end of the timespan.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    resampled : Period\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(pd.Period.asfreq)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
